package zj.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import zj.pojo.ZjObject;
import zj.utils.ZjFinal;

/**
 * Created by wjw on 2017/5/28.
 */

public class DBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "AsGhoul.db";
    private static final int DB_VERSION = 7;

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (String className : ZjFinal.classList){
            String sql = ZjObject.toCreateSQLString(className);
            try{
                db.execSQL(sql);
            }catch (Exception e){
                e.toString();
            }
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion){
            for (String className : ZjFinal.classList){
               String sql = ZjObject.toDropSQLString(className);
                try {
                    db.execSQL(sql);
                }catch (Exception e){
                    e.toString();
                }
            }
            onCreate(db);
        }

    }
    public void save(ZjObject zjObject){
        SQLiteDatabase db = this.getWritableDatabase();
        String className = zjObject.getClassName();
        ContentValues contentValues = zjObject.toContentValues();
        db.insert(className,null,contentValues);
        db.close();
    }

    public List<ZjObject> queryAll(Class<?> objectClass){
        List<ZjObject> zjObjectList = new ArrayList<ZjObject>();
        SQLiteDatabase db = this.getWritableDatabase();
        try {
            Cursor cursor = db.query(objectClass.getSimpleName(),null,null,null,null,null,null);
            while (cursor.moveToNext()){
                ZjObject zjObject = (ZjObject) objectClass.newInstance();
                ContentValues contentValues = zjObject.toContentValues();
                for (Map.Entry<String, Object> item : contentValues.valueSet())
                {
                    String key = item.getKey();
                    String value = cursor.getString(cursor.getColumnIndex(key));
                    if (key.equals("time")){
                        long time = cursor.getLong(cursor.getColumnIndex(key));
                        Date date = new Date(time);
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YY-mm-dd HH:mm:ss");
                        value = simpleDateFormat.format(date);
                    }
                    contentValues.put(key, value);
                }
                zjObject.contentValuesToObject(contentValues);
                zjObjectList.add(zjObject);
            }
        }catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }finally {
            db.close();
        }
        return zjObjectList;
    }
}
